package fr.cephb.locustree.loaders;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import fr.cephb.locustree.LTLoader;
import fr.cephb.locustree.MappedObject;

public abstract class AbstractLTLoader
	implements LTLoader
	{
	private static Pattern COMMA=Pattern.compile("[,]");
	private final static long DIFF_MILLISEC=1000L*60L;//1 minute
	protected Logger LOG=Logger.getLogger("fr.cephb");
	protected long nLine=0L;
	protected long inserted=0L;
	protected MappedObject mappedObject=null;
	protected String chromosome=null;
	private long lastTime=-1L;
	protected Set<String> keywords=new HashSet<String>();
	protected AbstractLTLoader()
		{
		
		}
	
	@Override
	public String getChromosome()
		{
		return this.chromosome;
		}
	
	@Override
	public MappedObject getMappedObject()
		{
		return this.mappedObject;
		}
	
	@Override
	public Set<String> getKeywords() {
		return this.keywords;
		}
	
	protected void time(String object)
		{
		if( (System.currentTimeMillis() - this.lastTime) > DIFF_MILLISEC)
			{
			if(this.nLine!=1L)
				{
				long freeMem= Runtime.getRuntime().freeMemory();
				LOG.info(
				"\nTotal Memory"+Runtime.getRuntime().totalMemory()+  
				"\nFree Memory"+freeMem+
				"\nline["+this.inserted+"/"+this.nLine+"] "+object+
				"\n#STATS,"+this.inserted+","+System.currentTimeMillis()+","+freeMem
				);
					
				}
			this.lastTime=System.currentTimeMillis();
			}
		}
	
	protected List<Integer> listOfInt(String s)
		{
		if(s==null || s.equals("")) return null;
		String tokens[]=COMMA.split(s);
		List<Integer> array= new ArrayList<Integer>(tokens.length);
		for(String token :tokens)
			{
			array.add(Integer.parseInt(token));
			}
		return array;
		}
	}
